package com.guogee.pdb.dao;

import com.guogee.pdb.bean.Service;
import com.guogee.pdb.db.DBHelper.ServicesCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class ServiceDAO extends ADao<Service> {

	public ServiceDAO() {
		dbCollection = dbHelper.getIsmartDB().getCollection(ServicesCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(Service obj) {
		if(insertItem1(obj)!=null){
			return true;
		}
		return false;
	}

	@Override
	public Service parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());

	}

	@Override
	public String insertItem1(Service obj) {
		// TODO Auto-generated method stub
		DBObject dbObj = new BasicDBObject();
		dbObj.put(ServicesCollection.NAME, obj.getName());
		dbObj.put(ServicesCollection.DURATION, obj.getDuration());
		dbObj.put(ServicesCollection.ACTIVATE_TIME, obj.getActivateTime());
		if(checkWritenResult(dbCollection.insert(dbObj))){
			return dbObj.get(ServicesCollection.ID).toString();
		}
		return null;
	}

	@Override
	public Service parstDBObject(DBObject object) {
		// TODO Auto-generated method stub
		Service ser = new Service();
		ser.setId(object.get(ServicesCollection.ID).toString());
		ser.setName(object.get(ServicesCollection.NAME).toString());
		ser.setDuration(Integer.valueOf(object.get(ServicesCollection.DURATION).toString()));
		ser.setActivateTime(Long.valueOf(object.get(ServicesCollection.ACTIVATE_TIME).toString()));
		return ser;
	}
}
